#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

const char *vertexShaderSource = "#version 330 core\n"
                                 "layout (location = 0) in vec3 aPos;\n"
                                 "void main()\n"
                                 "{\n"
                                 "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
                                 "}\0";
const char *fragmentShaderSource = "#version 330 core\n"
                                   "out vec4 FragColor;\n"
                                   "void main()\n"
                                   "{\n"
                                   "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
                                   "}\n\0";

void framebuffer_size_cb(GLFWwindow *window, int w, int h);
void processInput(GLFWwindow *window);
void init_glfw();

struct MyGLView
{
    int width;
    int height;
    GLFWwindow *window;
    const char *title;
    unsigned int vao;
    unsigned int vbo;
    unsigned int shader_program;

    MyGLView(int w, int h, const char *title_)
    {
        width = w;
        height = h;
        title = title_;

        window = glfwCreateWindow(w, h, title, NULL, NULL);
        if (is_valid())
        {
            glfwMakeContextCurrent(window);
            glfwSetFramebufferSizeCallback(window, framebuffer_size_cb);

            if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
            {
                window = NULL;
                std::cout << "Failed to load GLAD" << std::endl;
            }
        }
        else
        {
            std::cout << "Failed to create GLFW window" << std::endl;
        }
    }
    ~MyGLView()
    {
        if (is_valid())
        {
            glDeleteVertexArrays(1, &vao);
            glDeleteBuffers(1, &vbo);
            glDeleteProgram(shader_program);
        }
    }

    bool is_valid() { return window != NULL; }
    void prepare();
    void show();
};

int main()
{
    init_glfw();

    MyGLView gl_view(800, 600, "Hello Triangle");
    if (!gl_view.is_valid())
    {
        return -1;
    }
    gl_view.prepare();
    gl_view.show();

    return 0;
}

void MyGLView::prepare()
{
    unsigned int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertex_shader);
    unsigned int frame_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(frame_shader, 1, &fragmentShaderSource, NULL);
    glCompileShader(frame_shader);
    shader_program = glCreateProgram();
    glAttachShader(shader_program, vertex_shader);
    glAttachShader(shader_program, frame_shader);
    glLinkProgram(shader_program);

    float triangle[] = {
        -0.5, -0.5, 0,
        0.5, -0.5, 0,
        0, 0.5, 0};
    glGenVertexArrays(1, &vao);
    glGenBuffers(1, &vbo);
    glBindVertexArray(vao);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
    glEnableVertexAttribArray(0);

    glBindVertexArray(0);
}

void MyGLView::show()
{
    while (!glfwWindowShouldClose(window))
    {
        processInput(window);
        // render
        // ------
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(shader_program);
        glBindVertexArray(vao);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
}

void init_glfw()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}

void framebuffer_size_cb(GLFWwindow *window, int w, int h)
{
    glViewport(0, 0, w, h);
}

void processInput(GLFWwindow *window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}